package com.cyrilmottier.android.gdcatalog;

import java.io.File;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class LoginActivity extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);

		checkInitialization();

		Button btnLogin = (Button) findViewById(R.id.signin_button);
		btnLogin.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent tasksIntent = new Intent(LoginActivity.this, CatalogActivity.class);
				startActivity(tasksIntent);
			}
		});
	}

	public void checkInitialization() {
		if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) { // 判断手机SD卡是否存在
			if (new File("/sdcard").canRead()) {
				File file = new File("sdcard/sample-photos");
				if (!file.exists()) {
					file.mkdir();
					file = new File("sdcard/sample-photos/thumbnail_fatality");
					file.mkdir();
					file = new File("sdcard/sample-photos/fatality");
					file.mkdir();
				}
			}
		} else {
			new AlertDialog.Builder(this).setMessage("检查到没有存储卡,请插入手机存储卡再开启本应用").setPositiveButton("确定", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialoginterface, int i) {
					finish();
				}
			}).show();
		}

		
	}

	public void checkNetWorkStatus() {
		ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
		NetworkInfo networkinfo = connectivityManager.getActiveNetworkInfo();
		if (networkinfo == null || !networkinfo.isAvailable()) { // 当前网络不可用
			new AlertDialog.Builder(LoginActivity.this).setMessage("检查到没有可用的网络连接,请打开网络连接").setPositiveButton("确定", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialoginterface, int i) {
					ComponentName cn = new ComponentName("com.android.settings", "com.android.settings.Settings");
					Intent intent = new Intent();
					intent.setComponent(cn);
					intent.setAction("android.intent.action.VIEW");
					startActivity(intent);
				}
			}).show();
		}else{ // log
			Log.d("login", networkinfo.getTypeName());
		}
	}
}
